package gamecenter.gc.controller;

import gamecenter.gc.bean.*;
import gamecenter.gc.service.Comment_Service;
import gamecenter.gc.service.Game_Center_Service;
import gamecenter.gc.service.Game_Service;
import gamecenter.gc.service.Purchase_Order_Service;
import gamecenter.gc.service.Refund_Request_Service;
import gamecenter.gc.service.User_Game_Service;
import gamecenter.gc.service.User_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class User_Controller {
    @Autowired
    private Game_Center_Service game_center_service;
    @Autowired
    private User_Service user_service;
    @Autowired
    private User_Game_Service user_game_service;
    @Autowired
    private Purchase_Order_Service purchase_order_service;
    @Autowired
    private Refund_Request_Service refund_request_service;
    @Autowired
    private Comment_Service comment_service;
    @Autowired
    private Game_Service game_service;

    @RequestMapping(value = "register_user", method = RequestMethod.POST)
    public boolean register_user(@RequestBody User user) {
        String name = user.getName();
        String password = user.getPassword();
        return user_service.register(name, password);
    }

    @RequestMapping(value = "cer_user", method = RequestMethod.GET)
    public User get_Cer_User(String name, String password) {
        if (user_service.check_User(name, password))
            return user_service.get_Cer_User_From_Name(name);
        return null;
    }

    @RequestMapping(value = "get_user", method = RequestMethod.GET)
    public User get_User(String name) { return user_service.get_Cer_User_From_Name(name); }

    @RequestMapping(value = "modify_user_password", method = RequestMethod.GET)
    public boolean modify_User_Password(String name, String o_password, String n_password) {
        if (!user_service.check_User(name, o_password))
            return false;
        user_service.modify_User_Password(name, n_password);
        return true;
    }

    @RequestMapping(value = "modify_user_age", method = RequestMethod.POST)
    public boolean modify_User_Age(@RequestBody User user) {
        String name = user.getName();
        int age = user.getAge();
        user_service.modify_User_Age(name, age);
        return true;
    }

    @RequestMapping(value = "modify_user_photo", method = RequestMethod.PUT)
    public boolean modify_User_Photo(@RequestBody User user) {
        String name = user.getName();
        String password = user.getPassword();
        String photo = user.getPhoto();
        if (!user_service.check_User(name, password))
            return false;
        user_service.modify_User_Photo(name, photo);
        return true;
    }

    @RequestMapping(value = "user_recharge", method = RequestMethod.POST)
    public boolean user_Recharge(@RequestBody Recharge_Order order) {
        String name = order.getName();
        int money = order.getMoney();
        user_service.user_Recharge(name, money);
        return true;
    }

    @RequestMapping(value = "get_all_Game_From_User", method = RequestMethod.GET)
    public List<Game> get_all_Game_From_User(String name) {
//        if (!user_service.check_User(name, password))
//           return null;
        return user_game_service.get_all_Game_From_User(name);
    }

    @RequestMapping(value = "user_buy_games", method = RequestMethod.POST)
    public boolean user_Buy_Game(@RequestBody Purchase_Order order) {
        String user_name = order.getUser_name();
        String game_name = order.getGame_name();
        if (user_game_service.has_Game(user_name, game_name))
            return false;
        if (!game_service.check_Game_On(game_name)) {
            return false;
        }
        Game game = game_service.get_Cer_Game_From_Name(game_name);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        order.setTime(df.format(new Date()));// new Date()为获取当前系统时间
        order.setPrice(game.getPrice());
        User user = user_service.get_Cer_User_From_Name(user_name);
        if (order.getPrice() > user.getMoney())
            return false;
        purchase_order_service.add_Order(order);
        user_game_service.buy_Game(user_name, game_name);
        user_service.user_Purchase(user_name, order.getPrice());
//        purchase_order_service.add_Order(order);
        return true;
    }

    @RequestMapping(value = "user_refund", method = RequestMethod.GET)
    public boolean user_refund(String user_name, String game_name, String reason) {
        if (!user_game_service.has_Game(user_name, game_name))
            return false;
        if (refund_request_service.check_Request(user_name, game_name))
            return false;
        Refund_Request request = new Refund_Request(game_name);
        Game game = game_service.get_Cer_Game_From_Name(game_name);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        request.setTime(df.format(new Date()));// new Date()为获取当前系统时间
        request.setMoney(game.getPrice());
        request.setReason(reason);
        request.setUser_name(user_name);
//        user_game_service.del_Game(user_game, game_name);
        refund_request_service.add_Request(request);
        return true;

    }

    @RequestMapping(value = "add_comment", method = RequestMethod.POST)
    public boolean user_Add_Comment(@RequestBody Comment comment) {
        String user_name = comment.getUser_name();
        String game_name = comment.getGame_name();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        comment.setTime(df.format(new Date()));
        comment.setOn(true);

        if (!user_game_service.has_Game(user_name, game_name))
            return false;
        if (!comment.getUser_name().equals(user_name))
            return false;

        if (!comment_service.has_Comment(user_name, game_name)) {
            comment_service.add_Comment(comment);
        }
        else {
            comment_service.modify_Comment(comment);
        }
        return true;
    }

    @RequestMapping(value = "get_all_On_Comment", method = RequestMethod.GET)
    public List<Comment> get_all_On_Comment() {
        return comment_service.get_all_On_Comment();
    }

    @RequestMapping(value = "get_Game_Comment", method = RequestMethod.GET)
    public List<Comment> get_all_On_Comment(String game_name) {
        return comment_service.get_Game_Comment(game_name);
    }

    @RequestMapping(value = "del_Comment_user", method = RequestMethod.DELETE)
    public boolean user_Del_Comment(@RequestBody Comment comment, String user_name, String password) {
        String game_name = comment.getGame_name();
        if (!user_service.check_User(user_name, password))
            return false;
        if (user_game_service.has_Game(user_name, game_name))
            return false;
        if (!comment.getUser_name().equals(user_name))
            return false;

        if (!comment_service.has_Comment(user_name, game_name)) {
            return false;
        }
        else {
            comment_service.del_Comment(comment);
        }
        return true;
    }

}
